package com.hly.tool.file;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/6/30 9:36
 * @description：
 */
public class XMLUtil
{

    /**
     * @param xmlPath
     * @return
     */
    public static Element getXMLRootElelment(String xmlPath) throws DocumentException
    {
        Document document = getXMLDocument(xmlPath);
        Element root = document.getRootElement();
        return root;
    }

    /**
     * @param document
     * @return
     */
    public static Element getXMLRootElelment(Document document) throws DocumentException
    {
        Element root = document.getRootElement();
        return root;
    }


    /**
     * @param xmlPath
     * @return
     */
    public static Document getXMLDocument(String xmlPath) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(xmlPath);
        return document;
    }


    /**
     * @param xmlPath
     * @return
     */
    public static Document getXMLDocumentUTF8(String xmlPath) throws Exception
    {
        SAXReader reader = new SAXReader();
        FileInputStream fis = new FileInputStream(xmlPath);
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        Document document = reader.read(isr);
        return document;
    }

    /**
     * @param xmlPath
     * @return
     */
    public static Document getXMLDocumentChartSet(String xmlPath,String charset) throws Exception
    {
        SAXReader reader = new SAXReader();
        FileInputStream fis = new FileInputStream(xmlPath);
        InputStreamReader isr = new InputStreamReader(fis,charset);

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(xmlPath),charset));

        Document document = reader.read(br);

        return document;
    }



    public static void main(String[] args)
    {
//        try
//        {
//////            String fcFile = "C:\\Users\\KY08\\Desktop\\std_part_1.xml";
//////            String fcFile = "C:\\Users\\KY08\\Desktop\\标准件分组维护清单_error.xml";
//////            Document xmlDocument = getXMLDocumentChartSet("C:\\Users\\KY08\\Desktop\\std_part_1.xml","UTF-8");
////            SAXReader reader = new SAXReader();
////            Document document = reader.read(new BufferedInputStream(new FileInputStream(fcFile)));
////
////            Element root = getXMLRootElelment(document);
//////            String text = root.getText();
////            String Title = root.attributeValue("Title");
////
//////            System.out.println("text = "+text);
////            System.out.println("Title = "+Title);
////
////
////            List<Element> GeneralClassEles = root.elements("GeneralClass");
////            for (int i = 0; i < GeneralClassEles.size(); i++)
////            {
////                Element GeneralClassEle = GeneralClassEles.get(i);
////                System.out.println(GeneralClassEle.attributeValue("Title"));
////            }
//        } catch (Exception e)
//        {
//            e.printStackTrace();
//        }
    }

}
